package com.spnice.sgsms.service.impl;

import com.alibaba.fastjson.JSON;
import com.spnice.sgsms.enums.ResultEnum;
import com.spnice.sgsms.service.AnalysisService;
import com.spnice.sgsms.util.SettingUtils;
import com.spnice.sgsms.util.WrappedResult;
import com.spnice.sgsms.vo.SettingVo;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

@Service
@Slf4j
public class AnalysisServiceImpl implements AnalysisService {
    @Override
    public List<SettingVo> analysisPDF(MultipartFile file) {
        try {
            @Cleanup
            InputStream inputStream = file.getInputStream();
            return SettingUtils.analysisPDF(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void pdfToStandardExcel(MultipartFile file, HttpServletResponse response) {
        try {
            @Cleanup
            InputStream inputStream = file.getInputStream();
            String filename = file.getOriginalFilename();
            if (StringUtils.isBlank(filename)) {
                filename = "定值单.pdf";
            }
            byte[] bytes = IOUtils.toByteArray(inputStream);
            @Cleanup
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
            List<List<String>> pdfText = SettingUtils.getPDFText(byteArrayInputStream);
            @Cleanup
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            List<SettingVo> settingVos = SettingUtils.analysisPDF(bis);
            // 导出excel
            Workbook workbook = new XSSFWorkbook();
            // sheet1
            Sheet sheet1 = workbook.createSheet("sheet1");
            int k = 1;
            Row titleRow = sheet1.createRow(0);
            Cell titleCell1 = titleRow.createCell(0);
            titleCell1.setCellValue("组号");
            Cell titleCell2 = titleRow.createCell(1);
            titleCell2.setCellValue("定值组");
            Cell titleCell3 = titleRow.createCell(2);
            titleCell3.setCellValue("序号");
            Cell titleCell4 = titleRow.createCell(3);
            titleCell4.setCellValue("定值项描述");
            Cell titleCell5 = titleRow.createCell(4);
            titleCell5.setCellValue("定值");
            Cell titleCell6 = titleRow.createCell(5);
            titleCell6.setCellValue("单位");
            if (CollectionUtils.isNotEmpty(settingVos)) {
                for (SettingVo settingVo : settingVos) {
                    Row row = sheet1.createRow(k);
                    Cell cell1 = row.createCell(0);
                    Cell cell2 = row.createCell(1);
                    Cell cell3 = row.createCell(2);
                    Cell cell4 = row.createCell(3);
                    Cell cell5 = row.createCell(4);
                    Cell cell6 = row.createCell(5);
                    cell1.setCellValue(settingVo.getGroupOrderNumber());
                    cell2.setCellValue(settingVo.getGroupName());
                    cell3.setCellValue(settingVo.getOrderNumber());
                    cell4.setCellValue(settingVo.getDescription());
                    cell5.setCellValue(settingVo.getValue());
                    cell6.setCellValue(settingVo.getUnit());
                    k++;
                }
                for (int i = 0; i < 6; i++) {
                    sheet1.autoSizeColumn(i);
                    sheet1.setColumnWidth(i, sheet1.getColumnWidth(i) * 17 / 10);
                }
            }

            // sheet2
            Sheet sheet2 = workbook.createSheet("sheet2");
            int t = 0;
            for (List<String> textList : pdfText) {
                Row row = sheet2.createRow(t);
                int j = 0;
                for (String s : textList) {
                    Cell cell = row.createCell(j);
                    cell.setCellValue(s);
                    j++;
                }
                t++;
            }
            for (int i = 0; i < t; i++) {
                sheet2.autoSizeColumn(i);
//                    sheet2.setColumnWidth(i, sheet2.getColumnWidth(i) * 17 / 10);
            }
            String excelFileName = URLEncoder.encode(filename.replace(".pdf", ".xlsx"), "UTF-8");
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            response.setHeader("Content-Disposition", "attachment;filename="
                    + excelFileName);
            response.setHeader("excelFileName", excelFileName);
            workbook.write(response.getOutputStream());
            return;
        } catch (IOException e) {
            e.printStackTrace();
        }

        response.setContentType("application/json;charset=UTF-8");
        try {
            response.getWriter().write(JSON.toJSONString(WrappedResult.failedWrappedResult(ResultEnum.FILE_ANALYSIS_ERROR)));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
